<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="品种" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="项目ID/项目标题/项目内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="合约" prop="contract">
          <el-input v-model="filterForm.contract" clearable placeholder="合约关键字/合约ID/合约代码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="结算时间" prop="settlementDate">
          <el-date-picker 
          clearable
            v-model="filterForm.settlementDate"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-radio-group v-model="filterForm.status">
              <el-radio-button label="0">全部</el-radio-button>
              <el-radio-button label="2">持仓中</el-radio-button>
              <el-radio-button label="1">已平仓</el-radio-button>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号">
        <template slot-scope="{row}">
          <div @click="handleDetail(row.ID)" class="link">{{ row.localSymbol }}</div>
        </template>
      </el-table-column>
      <el-table-column label="合约" min-width="300px">
        <template slot-scope="{row}">
          <div @click="handleDetail(row.ID)" class="link">{{ row.symbol }}</div>
        </template>
      </el-table-column>
      <el-table-column label="成本价格" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="持仓" min-width="150px">
        <template slot-scope="{row}">
          <div>{{ row.quantity }}</div>
        </template>
      </el-table-column>
      <el-table-column label="资产" min-width="160px">
        <template slot-scope="{row}">
          <div>{{ row.amount }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最新价格" min-width="160px">
        <template slot-scope="{row}">
          <div>{{ row.marketValue }}</div>
        </template>
      </el-table-column>
      <el-table-column label="利润" min-width="160px">
        <template slot-scope="{row}">
          <div>{{ row.profit }}</div>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" min-width="160px">
        <template slot-scope="{row}">
          <div>{{ row.expireDate }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150px">
        <template slot-scope="{row}">
          <el-tag v-if="row.status.value == 1" type="danger" effect="dark">已平仓</el-tag>
          <el-tag v-else-if="row.status.value == 2" type="warning" effect="dark">持仓中</el-tag>
          <el-tag v-else effect="dark">未定义</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后更新" min-width="160px">
        <template slot-scope="{row}">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200px">
        <template slot-scope="{row}">
            <el-button type="text" @click="handleUpdate(row.ID)" >修改</el-button>
            <el-button type="text" v-if="row.inventoryStatus == 4" @click="handleInventoryRemove(row.ID)" >删除</el-button>
            <el-button type="default" @click="handleSecuritiesQuotationButton(row)" >市场行情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />


  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getInventoryList,copyInventory,removeInventory} from '@/api/fund/inventory'

export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      connected:"loading",
      depositForm:{
        amount:100
      },
      hiddenDepositWrapper:false,
      filterForm: {
        page: 1,
        size: 10,
        simulation: 2,
        status: '0'
      }
    }
  },
  created() {
    this.getList()
  },
  
  methods: {
    getList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getInventoryList(_this.filterForm).then((response) => {
        console.log(response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleSecuritiesQuotationButton:function(row){
        this.$router.push({ path: '/quotation/quotation', query: { 'symbol': row.symbol,"symbolType":3 }})
    },
    handleInventoryRemove:function(inventoryID){
      let _this = this;
      let formData = {
        "inventoryID":inventoryID
      }
      removeInventory(formData).then((response) => {
        console.log(response)
          if (response.code === 200) {
            _this.getList()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })

    },
    handleInventoryCopy:function(inventoryID){
      let _this = this;
      let formData = {
        "inventoryID":inventoryID
      }
      copyInventory(formData).then((response) => {
        console.log(response)
          if (response.code === 200) {
            _this.getList()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })

    },
    handleCreate:function(){
      this.$router.push({ path: 'inventory-add'})
    },
    handleUpdate:function(inventoryID){
        this.$router.push({ path: 'inventory-add', query: { 'inventoryID': inventoryID }})
    },
    handleDetail:function(inventoryID){
        this.$router.push({ path: 'inventory-detail', query: { 'inventoryID': inventoryID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    }
  }
}
</script>
<style>

</style>